|
|
|
|
|
|
|
Visual Basic represents a color as a long integer that is a combination of red, green, and blue components. The red, green, and blue values all have a numeric range between 0 and 255. If all three of these values are 0, the color contains no red, green, or blue, so the color is black. If all three of these values are 255, the color contains as much red, green, and blue as possible, and the color is white. |
|
|
|
|
|
|
|
|
The RGB function provided by Visual Basic combines red, green, and blue values and produces the long integer representing the combined color. The RGB function has this syntax: |
|
|
|
|
|
|
|
|
red, green, blue The amounts of red, green, and blue you want the color to have. |
|
|
|
|
|
|
|
|
The RGB function computes the color value using the formula: |
|
|
|
|
|
|
|
|
color = blue * &HFFOO& + green * &HFF& + red |
|
|
|
|
|
|
|
|
Using this information, you can decompose a color from its long integer representation into its red, green, and blue components, as shown in the following code: |
|
|
|
 |
|
|
|
|
r = color And &HFF&
g = (color And &HFF00&) \ &H100&
b = (color And &HFF0000) \ &H1000 |
|
|
|
|
|
|
|
|
You can specify a color by either using the RGB function or giving its numeric value. Hexadecimal makes specifying numeric values easier because each of a color's components occupies exactly two hexadecimal digits. If the color has red, green, and blue components of RR, GG, and BB in hexadecimal, you can represent the color as &HBBGGRR. For example, if a color has a red value of &H80, a green value of &H00, and a blue value of &HFF, the color value is &HFF0080. |
|
|
|
|
|
|
|
|
Visual Basic also has several predefined constants that give the values for some of the most commonly used colors. These values are shown in Table 1.4. |
|
|
|
|
|
|
|
|
You should use these color constants whenever possible. Your code will be easier to understand, and Visual Basic is more likely to be able to supply the exact colors you want. If your computer is set up to use color palettes, it can display only a limited number of colors at once. If you try to use too many different colors, you may exceed that number and you will not get exactly the colors you want. By using standard colors as much as possible, you reduce the total number of colors you need. |
|
|
|
|
|
|
|
|
You can use the RGB method to specify colors between 0 and &HFFFFFF. Values &H80000000 and larger represent system-defined colors instead of RGB values. These colors include such things as the color of menu bars and the title bar for active windows. |
|
|
|
|
|